﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace KinectServer.Util
{
    public class AsyncByteStream
    {
        private readonly Object locker = new Object();

        private bool isNew = false;

        private MemoryStream buffer = new MemoryStream();

        public AsyncByteStream()
        {

        }

        public void Write(int frameNumber, byte[] newBits)
        {
            lock (locker)
            {
                this.buffer.Position = 0;
                this.buffer.Write(newBits, 0, newBits.Length);
                this.isNew = true;
                Monitor.Pulse(locker);
            }
        }

        public MemoryStream Bits 
        {
            get
            {
                lock (locker)
                {
                    if (this.isNew == false)
                    {
                        Monitor.Wait(locker);
                    }

                    this.isNew = false;
                    return this.buffer;
                }
            }
        }

    }
}
